import { getGoodslist, goodscount } from "../../../require/api"

const state = {
    // 商品列表
    goodsList: [],
    // 商品总数
    count: 0,
    // 页码
    pageNum: 1,
    //条数
    pageSize: 3
}

const getters = {
    getGoods(state) {
        return state.goodsList
    },
    getCount(state) {
        return state.count
    },
    getpageNum(state) {
        return state.pageSize
    }
}
const mutations = {
    GETGOODSLIST(state, payload) {
        state.goodsList = payload
    },
    GETCOUNT(state, payload) {
        state.count = payload
    },
    //页码传来的数据
    GETPAGENUM(state, payload) {
        state.pageNum = payload
    }

}
const actions = {
    //商品列表
    getManageAction(context) {
        getGoodslist({
            size: state.pageSize,
            page: state.pageNum,
            // fid,
            // sid
        }).then((res) => {
            console.log(res, '商品列表数据');

            let list = res.list ? res.list : []
            // 解决非第一页时 并作为最后一条数据时页面显示空状态
            if (context.state.pageNum != 1 && list.length == 0) {
                // 重新触发页码修改 页码要-1
                context.dispatch('changePageAction', context.state.pageNum - 1)

            }
            
            context.commit("GETGOODSLIST", res.list)
        })
    },
    getgoodslist({commit}){
        getGoodslist().then(res=>{
            console.log(res,'goodslist----------');
        })
    },

    // 总数
    getCountAction({ commit }) {
        goodscount().then(res => {
            console.log(res.list[0], "商品总数");
            if (res.code == 200) {
                commit("GETCOUNT", res.list[0].total)
            }

        })
    },
    //切换页面更新列表方法
    changePageAction(context, payload) {
        // 改页码
        context.commit("GETPAGENUM", payload)

        // 更新列表 调用
        context.dispatch('getManageAction')


    }

}

export default {
    state,
    getters,
    mutations,
    actions,
    namespaced: true
}